﻿Public Class CCANCELLabelState2 : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CCANCELLabekState2.IsEnter")
        If CCommon.isInStr(task.ActionCd, "CANCEL") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase = Nothing
        Dim pdaTask As CPDATask2 = DirectCast(task, CPDATask2)
        Dim webMng As CWebManager = CWebManager.GetInstance
        Dim AppManager As CApplicationManager = CApplicationManager.GetInstance

        'Dim waitTask As CPersonal = New CPersonal(pdaTask)
        Debug.WriteLine("CCANCELLabekState2.DoExecute...START")


        Dim StateDao As CMasterDAO = New CMasterDAO
        Dim app As CApplication = CApplication.GetInstance()
        Dim sql As CSQLite = app.MasterDB()
        Dim trx As CTransaction = sql.GetTransaction

        '取消処理
        '取消したい物を読んでください。
        If task.ProcessText = "" Then
            pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_044, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
            task.ProcessText = task.ReadTextWait(-1)
        End If

        'ラベル全部読んでOKボタン押したら
        If task.ProcessText.StartsWith(CConst.CANCEL_OK) Then
            'MessageBox.Show("データ検索")
            'ボタン追加(次のステップラベルをボタンで表示する)
            pdaTask.WindowManager.ClearActionLabel()

            'ユニークコードがあるかどうか
            Dim retSetCdLst As ArrayList = StateDao.GetOrderCdInfo(trx, pdaTask.CancelLabelList)

            'Pdaのデータにはないのでサーバーと接続して見る
            If retSetCdLst Is Nothing Then
                '画面でデータがないです。と画面表示
                'MessageBox.Show("データがないです。 サーバーとの接続")
                Debug.WriteLine("一致するデータがないです。")

                CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                               Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_052, Color.White)
                task.ActionCd = "LABEL"
                CCommon.FlagManage("CANCEL", False)
            Else
                If retSetCdLst.Count = 0 Then
                    '画面でデータがないです。と画面表示
                    'MessageBox.Show("データは有り->Matchが出来ません。")
                    Debug.WriteLine("取り消しするデータを読んでないです。")
                    CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                            Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_052, Color.White)
                    task.ActionCd = "LABEL"
                    CCommon.FlagManage("CANCEL", False)
                Else

                    '画面に表示
                    For i As Integer = 0 To retSetCdLst.Count - 1
                        Dim record As COrderRecord = pdaTask.WindowManager.CreateReportRecord
                        '注文コードで検索して見
                        Dim retList As CResultSet = StateDao.GetSetCdOrderInfo(trx, retSetCdLst(i))

                        'MessageBox.Show("setCd-" & i & ":" & retSetCdLst(i))
                        record.RowNum = retList.GetString("row_num")
                        record.ShiporderCd = retList.GetString("shiporder_cd")
                        record.InvoiceCd = retList.GetString("invoice_cd")
                        record.OrderCd = retList.GetString("order_cd")      '-->システムの注文番号

                        record.ItemCd = retList.GetString("item_cd")
                        record.ItemName = retList.GetString("item_name")
                        record.SysItemCd = retList.GetString("sysitem_cd")
                        record.OrderQty = retList.GetInt("order_qty")

                        'record.ActionQty = StateDao.GetOrderData2(trx, record.OrderCd)   'データベースで探す
                        record.ActionQty = StateDao.GetOrderData2(trx, retList.GetString("set_cd"))   'データベースで探す
                        record.ShiporderQty = retList.GetInt("order_qty")


                        record.BoxQty = retList.GetInt("inBox_qty")
                        record.ReserveCd = retList.GetString("reserve_cd")   '-->内示品番
                        'record.SetCd = retList.GetString("set_cd")
                        record.SetCd = StateDao.GetLinkKey(trx, retList.GetString("set_cd"))

                        '条件で合うものだけリスト状に表示させる
                        pdaTask.WindowManager.AddRecord(record)
                        retList.Close()
                    Next
                    If retSetCdLst.Count > 0 Then
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.REPORTLIST_MODE)
                        '選択情報の初期化
                        'pdaTask.WindowManager.Set_m_IndexInit()
                        pdaTask.WindowManager.Set_m_ReportSelectIndex()
                        '選択してください。
                        pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_045, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
                        task.ActionCd = "CANCEL"
                    End If
                End If
            End If

            '今まで読んだラベルを消す
            pdaTask.CancelLabelList.Clear()
            'actionボタンも消す
            pdaTask.WindowManager.ClearActionLabel()

            'ラベル全部読んでOKボタン押したら
        ElseIf task.ProcessText.StartsWith(CConst.CANCEL_DEL) Then
            'MessageBox.Show("選択")
            Dim linkKeyCd As String = task.ProcessText.Replace(CConst.CANCEL_DEL, "")
            'MessageBox.Show("選択されたSETCD:" & linkKeyCd)
            Debug.WriteLine("選択されたSETCD:" & linkKeyCd)

            '1番 '選択されたデータの送信フラグを見て、送信済のものがあれがサーバーに
            '取り消すSET_CDを送ってサーバーにあるものも取り消す
            '2番 選択されてるSET_CDを実績テーブルで削除し
            '3番 ページを探して表示する

            '1番
            Dim sendFlg As Boolean = StateDao.GetSetCdSendFlg(trx, linkKeyCd)
            Dim setCd As String = StateDao.GetSetCd(trx, linkKeyCd)
            'MessageBox.Show("選択されたSETCD:" & setCd)
            If sendFlg Then
                '実績データを送信済み
                Dim cancelSet As ArrayList = New ArrayList
                cancelSet.Add(linkKeyCd & vbTab & Now)

                '設定ファイルで結果待ち、結果待ちしないか
                If AppManager.m_AppSetting.SrvRecv Then
                    pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_008, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
                    'MessageBox.Show("11")
                    '結果を待つ
                    Dim filePath As String = CApplicationManager.GetRealPath(CApplicationManager.SETTING_ACT_PATH, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                    'ファイル作成
                    Dim ret As String = CFiles.FileOutput2("SHP_CANCEL-" & System.Environment.TickCount.ToString(), filePath, cancelSet)
                    If webMng.CacelUploadResult(ret, CConst.CANCEL_UPLOAD) Then
                        '成功したら削除
                        System.IO.File.Delete(ret)
                    Else
                        '警告やエラーメッセージを出す--------->>START
                        CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_052, Color.White)
                        task.ActionCd = "LABEL"
                        CCommon.FlagManage("CANCEL", False)

                        'actionボタンも消す
                        pdaTask.WindowManager.ClearActionLabel()
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.LIST_MODE)

                        task.ProcessText = ""
                        nextState = GetNextState(task)
                        Return nextState
                        '警告やエラーメッセージを出す--------->>END
                    End If
                Else
                    '結果を待たさない
                    Dim filePath As String = CApplicationManager.GetRealPath(CApplicationManager.SETTING_ACT_PATH, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                    CBgControl.BgThread("SHP_CANCEL-" & System.Environment.TickCount.ToString(), "csv", filePath, cancelSet, CConst.CANCEL_UPLOAD)
                    CFileLog.WriteLog("照合中の取り消し", "1", CFileLog.EnumLogType.UPLOAD)

                End If
            Else
                '実績データを未送信
            End If

            '2番
            Dim strSetDao As CResultSet = StateDao.GetSetCdOrderInfo(trx, setCd)
            Dim setOrderCd As String = strSetDao.GetString("order_cd")
            Dim actQty As Integer = StateDao.GetActionQty2(trx, strSetDao.GetString("set_cd"))

            If Not strSetDao Is Nothing Then
                'For i As Integer = 0 To strSetDao.FieldCount - 1
                SyncLock pdaTask.SendLockObject
                    While strSetDao.Read()
                        If strSetDao.GetString("unique_cd") <> "" Then
                            trx.UpdateSQL("delete from unique_lblData where unique_cd='" & strSetDao.GetString("unique_cd") & "'")
                            trx.UpdateSQL("delete from unique_shData where unique_cd='" & strSetDao.GetString("unique_cd") & "'")
                        End If
                    End While
                    'Next
                    strSetDao.Close()
                End SyncLock
            End If

            'Dim shpActQty As Integer = StateDao.GetShiporderActionQty(trx, setOrderCd)
            Dim shpActQty As Integer = 0
            SyncLock pdaTask.SendLockObject
                ' CFileLog.WriteLog("エラー落ちるかもー０６")
                shpActQty = StateDao.GetOrderData(trx, setOrderCd)   'データベースで探す
            End SyncLock


            trx.UpdateSQL("delete from orderdata_tbl where set_cd='" & setCd & "'")
            trx.UpdateSQL("delete from orderdata_detail where set_cd='" & setCd & "'")

            trx.UpdateSQL("delete from lblData_tbl where set_cd='" & setCd & "'")
            trx.UpdateSQL("delete from lblData_detail where set_cd='" & setCd & "'")

            pdaTask.Hardware.nextC()


            '自動的にページに移動する
            'CFileLog.WriteLog("自動的にページ--START時間")
            Dim pageLogic As Boolean = True
            For i As Integer = 0 To pdaTask.WindowManager.GetRecordCount - 1
                Dim recordBean As COrderRecord = pdaTask.WindowManager.GetRecord(i)
                If recordBean.OrderCd = setOrderCd Then
                    pdaTask.WindowManager.SelectOrder(recordBean.OrderCd)
                    pageLogic = False
                    Exit For
                End If
                pageLogic = True
            Next
            If pageLogic Then
                SyncLock pdaTask.SendLockObject
                    pdaTask.WindowManager.SetPage(setOrderCd)
                End SyncLock
                'pdaTask.WindowManager.SelectOrder(setOrderCd)
            End If

            '画面用
            'MessageBox.Show("orderCd:" & strOrderCd)
            For j As Integer = 0 To pdaTask.WindowManager.GetRecordCount - 1
                Dim record As COrderRecord = pdaTask.WindowManager.GetRecord(j)
                If record.OrderCd = setOrderCd Then
                    record.ActionQty = record.ActionQty - actQty
                    If record.ActionQty = record.ShiporderQty Then
                        'record.Visible = False
                        pdaTask.WindowManager.DelRecord(record, True)
                    Else
                        pdaTask.WindowManager.SelectOrder(setOrderCd)
                        'record.Visible = True
                    End If
                    record.ActionBoxQty -= 1
                    Exit For
                    pageLogic = False
                End If
            Next

            SyncLock pdaTask.SendLockObject
                trx.UpdateSQL("update shiporder_tbl set action_qty=" & shpActQty - actQty & "  where order_cd='" & setOrderCd & "'")
            End SyncLock

            If pageLogic Then
                SyncLock pdaTask.SendLockObject
                    pdaTask.WindowManager.SetPage(setOrderCd)
                End SyncLock

                'pdaTask.WindowManager.SelectOrder(setOrderCd)
            End If

            '完全に照合成功



            task.ActionCd = "LABEL"

            '絞ったリストを消す
            Dim delRecord As ArrayList = New ArrayList()
            For i As Integer = 0 To pdaTask.WindowManager.GetReportRecordCount - 1
                Dim shpOrderData As COrderRecord = pdaTask.WindowManager.GetReportRecord(i)
                delRecord.Add(shpOrderData)
            Next
            For j As Integer = 0 To delRecord.Count - 1
                pdaTask.WindowManager.DelRecord(delRecord(j), True)
            Next
            'pdaTask.WindowManager.ReleaseReport()
            'actionボタンも消す
            pdaTask.WindowManager.ClearActionLabel()
            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.LIST_MODE)

            CPersonal.initPdaTaskSetting(pdaTask)


        ElseIf task.ProcessText = CConst.CNS_CLR Then
            '取り消しの取り消し
            '今まで読んだラベルを消す
            pdaTask.CancelLabelList.Clear()

            'actionボタンも消す
            Debug.WriteLine("取り消しの取り消し")
            pdaTask.WindowManager.ClearActionLabel()
            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.LIST_MODE)
            CCommon.FlagManage("CANCEL", False)

            task.ActionCd = "LABEL"
        Else
            If pdaTask.WindowManager.GetWindowMode = EnumWindowMode.LIST_MODE Then

                Dim barcdType As String = StateDao.CheckBarCdType(trx, task.ProcessText)
                Dim barNm As String = StateDao.GetBarcdName(trx, barcdType)
                If Not barNm = "" Then
                    '次読むラベルのボタンを表示
                    pdaTask.addWindowLabelBox(barNm, 2)
                    pdaTask.CancelLabelList.Add(task.ProcessText)
                End If
            End If

            task.ActionCd = "CANCEL"
        End If

        task.ProcessText = ""
        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CCANCELLabekState2.DoExit")
        Return True
    End Function


End Class
